/*
   Copyright (c) 2015 Mizar Tools Contributors (mizartools.org)

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
*/
/*  Contributors :
 *	2015-02-07 Marco Riccardi - initial implementation
 *
 */
package org.mizartools.dli.database;

import java.io.File;
import java.io.FileReader;
import java.io.LineNumberReader;
import java.util.LinkedHashMap;

import org.mizartools.dli.utility.DirectoryFactory;

public class Item {

	private static LinkedHashMap<String, Integer> itemToId;
	private static LinkedHashMap<Integer, String> idToItemId;

	private Item(){
		
	}
	
	public static Integer getId(String itemId) {
		if (itemToId == null) loadData();
		return itemToId.get(itemId); 
	}

	public static String getItem(Integer id) {
		if (idToItemId == null) loadData();
		return idToItemId.get(id); 
	}

	public static LinkedHashMap<String, Integer> getIdList() {
		if (itemToId == null) loadData();
		return itemToId; 
	}

	public static LinkedHashMap<Integer, String> getItemIdList() {
		if (idToItemId == null) loadData();
		return idToItemId; 
	}
	
	private static void loadData(){
		itemToId = new LinkedHashMap<String, Integer>();
		idToItemId = new LinkedHashMap<Integer, String>();
		try {
			File file = new File(DirectoryFactory.getDliDatabaseDirectory(), "item");
			FileReader fileReader = new FileReader(file);
			LineNumberReader lineNumberReader = new LineNumberReader(fileReader);
			int i = 0;
			while (lineNumberReader.ready()) {
				String s = lineNumberReader.readLine();
				i++;
				itemToId.put(s, i);
				idToItemId.put(i, s);
			}
			lineNumberReader.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	

}
